package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.Consumer;
import com.blueheart.music.service.ConsumerService;
import com.blueheart.music.tencent.TencentConst;
import com.blueheart.music.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    /**
     * 增加用户
     * @param request
     * @return
     */
    @PostMapping("/consumer/addConsumer")
    public ResultUtil addConsumer(HttpServletRequest request){
        String userName = request.getParameter(MyConst.USER_NAME).trim();
        String password = request.getParameter(MyConst.USER_PASSWORD).trim();
        String sex = request.getParameter(MyConst.SING_SEX).trim();
        String phoneNum = request.getParameter(MyConst.USER_PHONE).trim();
        String email = request.getParameter(MyConst.USER_EMAIL).trim();
        String birth = request.getParameter(MyConst.SING_BIRTH).trim();
        String introduction = request.getParameter(MyConst.SING_INTRODUCTION).trim();
        String location = request.getParameter(MyConst.SING_LOCATION).trim();
        String avator = request.getParameter(MyConst.AVATOR).trim();
        if (StringUtils.isEmpty(userName)){
            return  ResultUtil.error(BaseExceptionEnum.NAME_NULL);
        }
        if (StringUtils.isEmpty(password)){
            return  ResultUtil.error(BaseExceptionEnum.PASSWORD_NULL);
        }
        if (consumerService.findByUserName(userName)!=null){
            return  ResultUtil.error(BaseExceptionEnum.USER_EXIST);
        }
        //转换生日格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date=simpleDateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String md5 = MD5Util.getMD5(password);
        Consumer consumer = new Consumer();
        consumer.setUsername(userName);
        consumer.setPassword(md5);
        consumer.setSex(new Integer(sex));
        consumer.setPhoneNum(phoneNum);
        consumer.setEmail(email);
        consumer.setBirth(date);
        consumer.setIntroduction(introduction);
        consumer.setLocation(location);
        consumer.setAvator(avator);
        Boolean flag = consumerService.addConsumer(consumer);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.ADD_ERROR);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/consumer/deleteConsumer")
    public ResultUtil deleteConsumer(@RequestParam("id") Integer id){
        if (id<0){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Boolean flag = consumerService.removeConsumer(id);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.REMOVE_ERROR);
    }

    /**
     * 更新用户
     * @param request
     * @return
     */
    @PostMapping("/consumer/updateConsumer")
    public ResultUtil updateConsumer(HttpServletRequest request){
        String id = request.getParameter(MyConst.SING_ID).trim();
        String userName = request.getParameter(MyConst.USER_NAME).trim();
        String sex = request.getParameter(MyConst.SING_SEX).trim();
        String phoneNum = request.getParameter(MyConst.USER_PHONE).trim();
        String email = request.getParameter(MyConst.USER_EMAIL).trim();
        String birth = request.getParameter(MyConst.SING_BIRTH).trim();
        String introduction = request.getParameter(MyConst.SING_INTRODUCTION).trim();
        String location = request.getParameter(MyConst.SING_LOCATION).trim();
        String avator = request.getParameter(MyConst.AVATOR).trim();
        if (StringUtils.isEmpty(userName)){
            return  ResultUtil.error(BaseExceptionEnum.NAME_NULL);
        }
        System.out.println(consumerService.findByUserName(userName)!=null);
        System.out.println(consumerService.findByUserName(userName).getId());
        System.out.println(id);
        System.out.println(!consumerService.findByUserName(userName).getId().equals(new Integer(id)));
        if (consumerService.findByUserName(userName)!=null&&!consumerService.findByUserName(userName).getId().equals(new Integer(id))){
            return  ResultUtil.error(BaseExceptionEnum.USER_EXIST);
        }
        //转换生日格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date=simpleDateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Consumer consumer = new Consumer();
        consumer.setId(new Integer(id));
        consumer.setUsername(userName);
        consumer.setSex(new Integer(sex));
        consumer.setPhoneNum(phoneNum);
        consumer.setEmail(email);
        consumer.setBirth(date);
        consumer.setIntroduction(introduction);
        consumer.setLocation(location);
        consumer.setAvator(avator);
        Boolean flag = consumerService.reviseConsumer(consumer);
        if (flag){
            return ResultUtil.success();
        }
        return  ResultUtil.error(BaseExceptionEnum.UPDATE_ERROR);
    }

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/consumer/findAll")
    public ResultUtil findAll(){
        List<Consumer> all = consumerService.findAll();
        if (all.size()>0){
            return ResultUtil.success(all);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 通过id查询用户
     * @param id
     * @return
     */
    @PostMapping("/consumer/findById")
    public ResultUtil findById(@RequestParam("id") Integer id){
        if (id<0){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Consumer byId = consumerService.findById(id);
        if (byId!=null){
            byId.setPassword(null);
            return ResultUtil.success(byId);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 根据用户名查询用户
     * @param userName
     * @return
     */
    @PostMapping("/consumer/findByUserName")
    public ResultUtil findByUserName(@RequestParam("userName") String userName){
        if (StringUtils.isEmpty(userName)){
            return ResultUtil.error(BaseExceptionEnum.NAME_NULL);
        }
        Consumer byUserName = consumerService.findByUserName(userName);
        if (byUserName!=null){
            return ResultUtil.success(byUserName);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 获取Tencent关键字
     * @return
     */
    @GetMapping("/tencent")
    public ResultUtil ShowAllTencentConst(){
        String SecretId = TencentConst.SecretId;
        String SecretKey = TencentConst.SecretKey;
        String Bucket = TencentConst.Bucket;
        String Region = TencentConst.Region;
        String Key = TencentConst.Key;
        String StorageClass = TencentConst.StorageClass;
        String[] tencent={SecretId,SecretKey,Bucket,Region,Key,StorageClass};
        return ResultUtil.success(tencent);
    }

    /**
     * 用户登录
     * @param userName
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/consumer/login")
    public ResultUtil login(@RequestParam("userName") String userName, @RequestParam("password")String password, HttpSession session){
        if (StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Consumer consumer = consumerService.login(userName, password);
        session.setAttribute(MyConst.CONSUMER,consumer);
        consumer.setPassword("");
        return ResultUtil.success(consumer);
    }

    /**
     * 用户退出
     * @param session
     * @return
     */
    @GetMapping("/consumer/logout")
    public ResultUtil logout(HttpSession session){
        Consumer consumer = (Consumer)session.getAttribute(MyConst.CONSUMER);
        if (consumer!=null){
            session.removeAttribute(MyConst.CONSUMER);
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.LOGOUT_ERROR);
    }

    /**
     * 修改密码
     * @param password
     * @return
     */
    @PostMapping("/consumer/updatePassword")
    public ResultUtil updatePassword(@RequestParam("id") Integer id,@RequestParam("password") String password){
        Consumer consumer = new Consumer();
        consumer.setId(id);
        consumer.setPassword(MD5Util.getMD5(password));
        Boolean flag = consumerService.reviseConsumer(consumer);
        if (flag){
            return ResultUtil.success();
        }
        return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
}
